package com.efounder.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.efounder.activity.ToEvaluateActivity;
import com.efounder.bean.CommentBean;
import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.mobilemall.R;
import com.efounder.pansoft.chat.photo.JFPicturePickPhotoWallActivity;
import com.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import static com.efounder.pansoft.chat.photo.JFMessagePicturePickView.REQUEST_PIC_SELECTE_CODE;

public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<CommentBean.GoodListBean> commentGoodList;
    private OnPositionChangedListener onPositionChangedListener;
    private OnSendCommentListener onSendCommentListener;

    public CommentAdapter(Context context, List<CommentBean.GoodListBean> list) {
        this.context = context;
        this.commentGoodList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CommentHolder(LayoutInflater.from(context).inflate(R.layout.mall_item_comment,
                parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ((CommentHolder)holder).comment_text.setText(commentGoodList.get(holder.getAdapterPosition()).getCommentContent());
        ((CommentHolder)holder).comment_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                commentGoodList.get(holder.getAdapterPosition()).setCommentContent(s.toString());
            }
        });
        ((CommentHolder)holder).mRatingBar.setRating(5);
        ((CommentHolder)holder).mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                switch ((int) rating) {
                    case 1:
                        ((CommentHolder)holder).text_comments.setText(R.string.mail_very_dissatisfied);
                        commentGoodList.get(holder.getAdapterPosition()).setStar(1);
                        break;
                    case 2:
                        ((CommentHolder)holder).text_comments.setText(R.string.mail_disappointment);
                        commentGoodList.get(holder.getAdapterPosition()).setStar(2);
                        break;
                    case 3:
                        ((CommentHolder)holder).text_comments.setText(R.string.mail_need_improve);
                        commentGoodList.get(holder.getAdapterPosition()).setStar(3);
                        break;
                    case 4:
                        ((CommentHolder)holder).text_comments.setText(R.string.mail_like);
                        commentGoodList.get(holder.getAdapterPosition()).setStar(4);
                        break;
                    case 5:
                        ((CommentHolder)holder).text_comments.setText(R.string.mail_very_like);
                        commentGoodList.get(holder.getAdapterPosition()).setStar(5);
                        break;
                    default:
                        ((CommentHolder)holder).text_comments.setText(R.string.mail_very_like);
                        commentGoodList.get(holder.getAdapterPosition()).setStar(5);
                        break;

                }
            }
        });

        LXGlideImageLoader.getInstance().displayImage(context, ((CommentHolder)holder).ivGood, commentGoodList.get(holder.getAdapterPosition()).getGoodcover());

        final PictureGridAdapter adapter = new PictureGridAdapter(context, commentGoodList.get(holder.getAdapterPosition()).getmPictureList());
        adapter.setOnImgRemovedListener(new PictureGridAdapter.OnImgRemovedListener() {
            @Override
            public void onImgRemoved(int position) {
                commentGoodList.get(holder.getAdapterPosition()).getmPictureList().remove(position);
                adapter.notifyDataSetChanged();
            }
        });
        ((CommentHolder)holder).gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onPositionChangedListener != null) {
                    onPositionChangedListener.onPositionChanged(holder.getAdapterPosition());
                }
                //加号，选择图片
                if (position == commentGoodList.get(holder.getAdapterPosition()).getmPictureList().size()) {
                    Intent intent = new Intent(context, JFPicturePickPhotoWallActivity.class);
                    ArrayList<String> tempList = new ArrayList<>();
                    for (int i = 0; i < commentGoodList.get(holder.getAdapterPosition()).getmPictureList().size(); i++) {
                        tempList.add(commentGoodList.get(holder.getAdapterPosition()).getmPictureList().get(i).getCloudFilePath());
                    }
                    intent.putExtra("mPictureList", tempList);
                    intent.putExtra(JFPicturePickPhotoWallActivity.MAX_NUM, 9);//可选择的最大照片数
                    intent.putExtra(JFPicturePickPhotoWallActivity.SHOW_RAW_PIC, false);//不显示原图
                    intent.putExtra("sendBtnName",  context.getResources().getString(R.string.common_text_confirm));
                    intent.putExtra(JFPicturePickPhotoWallActivity.SELECT_MODE, JFPicturePickPhotoWallActivity.MODE_PIC_ONLY);
                    ((ToEvaluateActivity)context).startActivityForResult(intent, REQUEST_PIC_SELECTE_CODE);
                }
            }
        });
        ((CommentHolder)holder).gridView.setAdapter(adapter);

        ((CommentHolder)holder).btnSendComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSendCommentListener != null) {
                    String text = ((CommentHolder)holder).comment_text.getText().toString();
                    if ("".equals(text)) {
                        ToastUtils.showShort(R.string.mall_input_comment);
                        return;
                    }
                    commentGoodList.get(holder.getAdapterPosition()).setCommentContent(text);
                    onSendCommentListener.onSendComment(holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return commentGoodList.size();
    }

    class CommentHolder extends RecyclerView.ViewHolder {
        GridView gridView;
        TextView comment_text;
        RatingBar mRatingBar;
        TextView text_comments;
        ImageView ivGood;
        Button btnSendComment;

        public CommentHolder(View itemView) {
            super(itemView);
            gridView = itemView.findViewById(R.id.gridView);
            comment_text = itemView.findViewById(R.id.comment_text);
            mRatingBar = itemView.findViewById(R.id.ratingBar);
            text_comments = itemView.findViewById(R.id.text_comments);
            ivGood = itemView.findViewById(R.id.goodpic);
            btnSendComment = itemView.findViewById(R.id.btn_send_comment);
        }
    }

    public interface OnPositionChangedListener{
        void onPositionChanged(int position);
    }

    public void setOnPositionChangedListener(OnPositionChangedListener onPositionChangedListener) {
        this.onPositionChangedListener = onPositionChangedListener;
    }

    public interface OnSendCommentListener{
        void onSendComment(int position);
    }

    public void setOnSendCommentListener(OnSendCommentListener onSendCommentListener) {
        this.onSendCommentListener = onSendCommentListener;
    }

}
